package com.sky.controller.User;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.UserLoginDTO;
import com.sky.entity.User;
import com.sky.properties.JwtProperties;
import com.sky.result.Result;
import com.sky.service.UserService;
import com.sky.utils.JwtUtil;
import com.sky.vo.UserLoginVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user/user")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    private JwtProperties jwtProperties;



    @PostMapping("/login")
    public Result login(@RequestBody UserLoginDTO code) {
        System.out.println("我是code{}"+code);

        User kk = userService.Login(code);

        Map<String,Object> cc = new HashMap<>();
        cc.put(JwtClaimsConstant.USER_ID,kk.getId());
        String tk = JwtUtil.createJWT(jwtProperties.getUserSecretKey(),jwtProperties.getUserTtl(),cc);

        UserLoginVO res = UserLoginVO.builder()
                .id(kk.getId())
                .openid(kk.getOpenid())
                .token(tk)
                .build();

        return Result.success(res);
    }

}
